<template>
  <div v-text="getQuote()" />
</template>
<!--外键组件-->
<script>
import store from '@/store'
import {ComponentEventType, EventBus, EventType} from "@/event";
function foreignChange({val, meta, model}) {
  let strage = store.state['store'];
  let tableList = strage['tableList'];
  model['fieldType'] = null;
  model['quoteTable'] = val[0]; model['foreignId'] = val[1];
  if(tableList instanceof Array) {
    if(val[1]) {
      // 查找对应外键的表
      let table = tableList.find(item => item.value == val[0]);
      if(table) {
        // 克隆一份表对象, 去除disabled字段的影响
        let cloneTable = JSON.parse(JSON.stringify(table));

        // 移除修改前的外键
        if(meta['quote'] instanceof Array) {
          let table = tableList.find(item => item.value == meta['quote'][0]);
          // 切换外键表后需要取消禁用状态
          if(table && table.value != val[0]) {
            table['disabled'] = false;
          }

          let findIndex = strage['foreignList'].findIndex(item => item.value == meta['quote'][0]);
          if(findIndex > -1) {
            strage['foreignList'].splice(findIndex, 1);
          }
        }

        let foreign = strage['foreignList'].find(item => item.value == val[0]);
        let column = table['children'].find(item => item.value == val[1]);

        model['fieldType'] = column.type; // 设置此列的类型
        model['target'] = column.target; // 目标列的元model对象
        if(column.target && column.target['title']) {
          model['title'] = column.target['title'];
          EventBus.publish(EventType.formTitle, {val: model['title'], id: meta['id']});
        }
        // 添加最新的外键
        if(!foreign) {
          meta['quote'] = val;
          table['disabled'] = true;

          strage['foreignList'].push(cloneTable);
        }
      }
    } else { // 移除外键
      let table = tableList.find(item => item.value == meta['quote'][0]);
      if(table) { // 移除外键后需要取消禁用状态
        table['disabled'] = false;
      }

      let foreignIndex = strage['foreignList'].findIndex(item => item.value == meta['quote'][0]);
      // 移除外键
      if(foreignIndex > -1) {
        strage['foreignList'].splice(foreignIndex, 1);
      }
    }
  }
}
function quoteChange({val, meta, model}) {
  model['field'] = val[1]; model['quoteTable'] = val[0]; model['fieldType'] = null;
  let strage = store.state['store'];
  let foreignList = strage['foreignList'];
  if(foreignList instanceof Array) {
    let foreign = strage['foreignList'].find(item => item.value == val[0]);
    if(foreign) {
      let column = foreign['children'].find(item => item.value == val[1]);
      if(column) {
        model['fieldType'] = column.type;
        model['target'] = column.target; // 目标列的元model对象

        if(column.target && column.target['title']) {
          model['title'] = column.target['title'];
          EventBus.publish(EventType.formTitle, {val: model['title'], id: meta['id']});
        }
      }
    }
  }
}
function cascadeVisibleChange(meta) {

}
export default {
  name: "IvzForeignForm",
  props: ['meta', 'model'],
  created() {
    // 新增外键事件
    let foreignChangeEvents = EventBus.allEvents().get(EventType.changeForeign);
    if(foreignChangeEvents == null) {
      EventBus.listener(EventType.changeForeign, foreignChange);
    } else {
      let findIndex = foreignChangeEvents.findIndex(cfn => cfn == foreignChange);
      if(findIndex == -1) {
        EventBus.listener(EventType.changeForeign, foreignChange);
      }
    }

    // 新增外键引用监听事件
    let quoteChangeEvents = EventBus.allEvents().get(EventType.changeQuote);
    if(quoteChangeEvents == null) {
      EventBus.listener(EventType.changeQuote, quoteChange);
    } else {
      let findIndex = quoteChangeEvents.findIndex(cfn => cfn == quoteChange);
      if(findIndex == -1) {
        EventBus.listener(EventType.changeQuote, quoteChange);
      }
    }

    // 加载外键表的列数据
    if(!EventBus.allEvents().has(EventType.loadingForeignColumn)) {
      EventBus.listener(EventType.loadingForeignColumn, options => {
        options['loading'] = true;
        this.$http('/lcd/design/columns', {params: {tableName: options.value}}).then(resp => {
            if(resp.code == 200) {
              options['children'] = resp['data'];
            } else {
              this.$msg.errorMessage(resp.message)
            }
          }).catch(reason => console.error(reason))
          .finally(() => options['loading'] = false)
      })
    }

  },
  methods: {
    getQuote() {
      let quoteValue = this.model['foreign'] || this.model['quote']
      if(this.model.type == 'foreign') {
        return quoteValue ? `一对一 [${this.model.field} -> ${quoteValue.join(":")}]` : `一对一 []`;
      } else if(this.model.type == 'foreignToMany') {
        return quoteValue ? `一对多 [${this.model.field} -> ${quoteValue.join(":")}]` : `一对多 []`;
      } else {
        return quoteValue ? `引用 [${this.model.field} -> ${quoteValue.join(":")}]` : `引用 []`;
      }
    }
  },
  unmounted() {
    let strage = this.$store.state['store'];
    // 移除外键从列表里面
    if(this.meta.type == 'foreign') {
      let val = this.meta['quote'];
      if(val instanceof Array) {
        let table = strage['tableList'].find(item => item.value == val[0]);
        if(table) { // 取消表的禁用状态
          table['disabled'] = false;
        }
        let findIndex = strage['foreignList'].findIndex(item => item.value == val[0]);
        if(findIndex > -1) {
          strage['foreignList'].splice(findIndex, 1);
        }
      }
    }

  }
}
</script>

<style scoped>

</style>
